package com.jkoss.wine.bulletin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.Constant;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.bulletin.entity.Bulletin;
import com.jkoss.wine.bulletin.service.IBulletinService;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.entity.MessageNotification;
import com.jkoss.wine.system.service.IMerchantEmployeesService;
import com.jkoss.wine.system.service.IMessageNotificationService;

/**
 * 公司公告 前端控制器
 * 
 * @Author 周华军
 * @Version 1.0, 2018-10-31
 * @See
 * @Since com.jkoss.wine.bulletin.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/bulletin/bulletin")
public class BulletinController extends BaseController {

	@Autowired
	private IBulletinService iBulletinService;
	@Autowired
	private IMerchantEmployeesService iMerchantEmployeesService;
	@Autowired
	private IMessageNotificationService iMessageNotificationService;

	/**
	 * 列表
	 * 
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/bulletin/bulletin/list")
	public String list(String title, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		String string = request.getParameter("org2.title");
		// 前端框架bean
		dwzPageBean.getCountResultMap().put("title", string);
		Wrapper wrapper = Condition.create().ne("b.del", 1).like("b.title", string);
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("b.add_time", false);
		}
		Page resultPage = iBulletinService.selectVoByPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));

		Wrapper updatewrapper = Condition.create().eq("meid", getUserId()).eq("type", 1);
		MessageNotification entity = new MessageNotification();
		// 类型：1-通知公告未读提醒、2-周报、3-月报未读及未提交提醒、4-新客户资源分配提醒、5-资源客户未沟通提醒
		entity.setType(1);
		// 是否已读：0-未读、1-已读
		entity.setIsread(1);
		iMessageNotificationService.update(entity, updatewrapper);
		return "bulletin/bulletin/list";
	}

	/**
	 * 去新增
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/bulletin/bulletin/toInsert")
	public String toInsert(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {

		Wrapper wrapper = Condition.create().ne("del", 1);
		Bulletin bulletin = iBulletinService.selectById(id);
		List<MerchantEmployees> merchantEmployees = iMerchantEmployeesService.selectList(wrapper);
		map.put("bulletin", bulletin);
		map.put("merchantEmployees", merchantEmployees);
		return "bulletin/bulletin/edit";

	}

	@RequestMapping("/insert")
	@RequiresPermissions("/bulletin/bulletin/toInsert")
	@ResponseBody
	public Object insert(@Valid Bulletin bulletin, HttpServletRequest request, HttpServletResponse response) {
		// 添加时间
		bulletin.setAddTime(CommonUtil.getDate());
		// 发布时间
		bulletin.setReleaseTime(CommonUtil.getDate());
		// 获取当前操作人
		bulletin.setOperator(getUserId());
		// 获取当前发布人ID
		bulletin.setEmployeeId(getUserId());
		// 是否删除：0-未删除、1-已删除、2-不能删除
		bulletin.setDel(0);
		if (iBulletinService.insert(bulletin)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/bulletin/bulletin/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {

		Wrapper wrapper = Condition.create().ne("del", 1);
		List<MerchantEmployees> merchantEmployees = iMerchantEmployeesService.selectList(wrapper);
		map.put("record", iBulletinService.selectById(id));
		map.put("merchantEmployees", merchantEmployees);
		return "bulletin/bulletin/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/bulletin/bulletin/toUpdate")
	@ResponseBody
	public Object update(@Valid Bulletin bulletin, HttpServletRequest request, HttpServletResponse response) {
		if (iBulletinService.updateById(bulletin)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/bulletin/bulletin/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		List list = new ArrayList<>();
		for (String string : id) {
			Bulletin bulletin = new Bulletin();
			bulletin.setId(string);
			// 是否删除 0-未删除，1-已删除
			bulletin.setDel(1);
			list.add(bulletin);
		}
		if (iBulletinService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 详情
	 * 
	 * @param title
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/details")
	@RequiresPermissions("/bulletin/bulletin/details")
	public String details(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper = Condition.create().ne("b.del", 1).eq("b.id", id);
		map.put("record", iBulletinService.selectVoOne(wrapper));
		return "bulletin/bulletin/details";
	}

	/**
	 * 查询标题
	 * 
	 * @param title
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/Bulletintitle")
	@ResponseBody
	public Object Bulletintitle(String title, String id, HttpServletRequest request, HttpServletResponse response) {

		Wrapper wrapper = Condition.create().like("title", title).ne("del", 1);
		wrapper.setSqlSelect("title");
		List<Bulletin> list = iBulletinService.selectList(wrapper);
		return JSON.toJSONString(list);

	}

}
